package main

import "fmt"

type ListNode struct {
	Val int
	Next *ListNode
}


func main() {
	l1 := &ListNode{Val: 2, Next: &ListNode{Val: 4,Next: &ListNode{Val: 3, Next: nil}}}
	l2 := &ListNode{Val: 5, Next: &ListNode{Val: 6,Next: &ListNode{Val: 4, Next: nil}}}
	result := addTwoNumbers(l1, l2)
	for {
		if result==nil{
			break
		}
		fmt.Print(result.Val, " ")
		result=result.Next
	}
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	result := &ListNode{Val: 0, Next: nil}
	p := result
	var num int
	for l1!=nil || l2!=nil{
		if p.Next != nil{
			p = p.Next
		}
		var num1, num2 int
		if l1!=nil{
			num1 = l1.Val
			l1 = l1.Next
		}
		if l2!=nil{
			num2 = l2.Val
			l2 = l2.Next
		}
		num += num1+num2
		if num>=10{
			p.Val = num - 10
			num = 1
		}else {
			p.Val = num
			num = 0
		}
		p.Next = &ListNode{Val: 0, Next: nil}
	}
	if num == 0{
		p.Next = nil
	}else{
		p.Next.Val = num
	}
	return result
}